package com.iwhalecloud.citybrain.flow.platform.manager.application.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @author cheng.huaxing
 * @date 2019-01-23
 */
@Slf4j
public class DateUtils {

    private final static DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private final static DateTimeFormatter START_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00");

    private final static DateTimeFormatter END_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59");

    private DateUtils() {
    }


    public static String dateToString(Date date) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return DEFAULT_FORMATTER.format(dateTime);
    }

    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static LocalDateTime stringToDate(String date) {
        return LocalDateTime.parse(date, DEFAULT_FORMATTER);
    }

    public static String startTimeFormatter(String startTime) {
        if (StringUtils.isBlank(startTime)) {
            return null;
        }
        LocalDate localDate = LocalDate.parse(startTime);
        return START_FORMATTER.format(localDate);
    }

    public static String endTimeFormatter(String endTime) {
        if (StringUtils.isBlank(endTime)) {
            return null;
        }
        LocalDate localDate = LocalDate.parse(endTime);
        return END_FORMATTER.format(localDate);
    }

    public static String timeFormatter(String time, String oldFormatter, String formatter) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        LocalDate localDate = LocalDate.parse(time, DateTimeFormatter.ofPattern(oldFormatter));
        return DateTimeFormatter.ofPattern(formatter).format(localDate);
    }

    /**
     * 获取一个时间的相对时间
     *
     * @param date
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date getRelativeTime(Date date, Integer day, Integer hour, Integer minute, Integer second) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        dateTime = dateTime.plusDays(day).plusHours(hour).plusMinutes(minute).plusSeconds(second);
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static Date string2OldDate(String date) {
        return localDateTime2Date(LocalDateTime.parse(date, DEFAULT_FORMATTER));
    }

    public static String getNewTimeByTimeType(Date datetime, int addType, String addValue, String pattern) {
        if(Objects.isNull(datetime) || StringUtils.isBlank(addValue)) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Calendar newTime = Calendar.getInstance();
        newTime.setTime(datetime);
        newTime.add(addType,Integer.parseInt(addValue));
        Date newDate = newTime.getTime();
        return formatter.format(newDate);
    }

    public static String dateToStr(Date datetime, String pattern) {
        if(datetime == null) {
            return null;
        }
        return DateUtil.format(datetime, pattern);
    }

    public static String utcTimeToBjStr(String dateTimeStr, String sourcePattern1, String targetPattern2) {
        if(StringUtils.isBlank(dateTimeStr) || StringUtils.isBlank(sourcePattern1) || StringUtils.isBlank(targetPattern2)) {
            return null;
        }

        // 计算时间
        try {
            //输入的被转化的时间格式
            SimpleDateFormat sdf1 = new SimpleDateFormat(sourcePattern1);
            Date date1 = sdf1.parse(dateTimeStr);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(targetPattern2);
            return simpleDateFormat.format(date1);
        } catch (ParseException e) {
            log.info("入参【"+dateTimeStr+"】 utcTimeToBjStr 转换异常:");
        }
        return null;
    }

    /**
     * 字符串转换成时间
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date strToDate(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
        }
        return null;
    }

    public static Date  TimestampToDate(String timestampStr) {
        try {
            long timestamp = Long.parseLong(timestampStr); // 将字符串转换为长整型
            Instant instant = Instant.ofEpochMilli(timestamp); // 转换为Instant
            Date date = Date.from(instant); // 从Instant转换为Date对象
            return date;
        } catch (Exception e) {
        }
        return null;
    }
}
